package com.example.kuaidigui;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import java.net.URL;
import java.net.URLConnection;

public class WelActivity extends Activity {
	private EditText userName1,password1;
	private Button btn_qj,btn_tc;
	private String userName1Value,password1Value;
	private SharedPreferences sp;
	private int i_typ = 0;
	private static final String TAG = "yeelink";
	protected void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.wel);
		sp=this.getSharedPreferences("userInfo", Context.MODE_WORLD_READABLE);
		userName1=(EditText)findViewById(R.id.et_gh);
		password1=(EditText)findViewById(R.id.et_mima1);
		btn_qj=(Button)findViewById(R.id.btn_qj);
		btn_tc=(Button)findViewById(R.id.btn_tc);
		//取件监听事件，现在默认用户名和密码
		btn_qj.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				userName1Value=userName1.getText().toString();
				password1Value=password1.getText().toString();
				if(userName1Value.equals("001")&&password1Value.equals("123456")){
					i_typ = 1;
					new Thread(networkTask).start();
					Toast.makeText(WelActivity.this,"取件成功",Toast.LENGTH_SHORT).show();
				}else{
					Toast.makeText(WelActivity.this, "柜号或密码错误，请重新登陆",Toast.LENGTH_LONG).show();
				}
			}
		});
		//监听取件成功事件
		btn_tc.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				i_typ = 0;
				new Thread(networkTask).start();
				Toast.makeText(WelActivity.this, "感谢使用", Toast.LENGTH_SHORT).show();
			}
		});

	}

	private String set_post(int i_typ) throws IOException {
		try {
			/**默认的设备id, 设置后, 调用upload方法时device_id可以填0*/
			int DFT_DEV_ID = 345282;	//343303;
			/**默认的传感器id, 设置后, 调用upload方法时sensor_id可以填0*/
			int DFT_SEN_ID = 384289;	//384302;
			Yeelinks.UAPIKEY = "6b5c8eb6a64a1cdc938502cd62ccbf9f";		//"012bd74e1c5e53311606e47605ad358b";
			Yeelinks.upload(DFT_DEV_ID, DFT_SEN_ID, String.valueOf(i_typ));
			return "true";
		} catch (Exception ex){
			return "error";
		}
	}

	public String getVal(String s1, String key){
		s1 = s1.replace("{", "");
		s1 = s1.replace("}", "");
		s1 = s1.replace("[", "");
		s1 = s1.replace("]", "");
		//s1 = s1.replace("\"", "");
		String[] s2 = s1.split(",");
		for(int i=0;i<s2.length;i++){
			String[] s3 = s2[i].split(":");
			String temp = s3[0].trim().replace("\"", "");
			String val = s3[1].trim().replace("\"", "");
			Log.i(TAG, temp + "=" + val);
			if(temp.compareTo(key) == 0){
				return val;
			}
		}
		return "";
	}

	Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			Bundle data = msg.getData();
			String val = data.getString("value");

			Log.i(TAG, "请求结果为-->" + val);


		}
	};

	/**
	 * 网络操作相关的子线程
	 */
	Runnable networkTask = new Runnable() {

		@Override
		public void run() {
			try {
				String val = set_post(i_typ);
			} catch (IOException e) {
				e.printStackTrace();
			}
			Message msg = new Message();
			Bundle data = new Bundle();
			try {
				data.putString("value", set_post(i_typ));
			} catch (IOException e) {
				e.printStackTrace();
			}
			msg.setData(data);
			handler.sendMessage(msg);
		}
	};

}
